package com.letu.core.utils;

import com.letu.core.context.ContextConstants;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * string转long
 *
 * @author ligang
 * @create 2023/10/26 19:59
 */
public class StringTLongUtil {
    public static MessageDigest messageDigest;

    static {
        try {
            messageDigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    private final static char[] hexArray = "0123456789ABCDEF".toCharArray();



    private final static char[] hexArrayOf20 = "0123456789ABCDEFGHIJ".toCharArray();


//    public static void main(String[] args) {
//        long l = System.currentTimeMillis();
//        Map<Integer,Integer> map = new HashMap();
//        map.put(0, 0);
//        map.put(1, 0);
//        map.put(2, 0);
//        map.put(3, 0);
//        map.put(4, 0);
//        map.put(5, 0);
//        String s = UUID.fastUUID().toString();
//        for (int i = 0; i < 50000; i++) {
//            Integer index = bigRemainder(s,5);
//            map.put(index, map.get(index)+1);
//        }
//        System.out.println(System.currentTimeMillis() -l);
//        System.out.println(JSONObject.toJSONString(map));
//    }

    /**
     * 大数据取余
     *
     * @param str
     * @return
     */
    public static Integer bigRemainder(String str, Integer suffix) {
        BigInteger bigInteger = stringToLong(str);
        Long l = bigInteger.longValue()%suffix;
//        BigInteger bigIntegers = bigInteger.remainder(new BigInteger(String.valueOf(suffix)));
//        return Integer.parseInt(bigIntegers.toString());
        int num = l.intValue();
        if(num<0){
            return Math.abs(num);
        }
        return num;
    }

    public static BigInteger stringToLong(String str) {
        byte[] md5hex =null;
        if(str.length()>=24){
            md5hex = messageDigest.digest(str.getBytes());
            return new BigInteger(bytesToHexOf24(md5hex), 20);
        }else{
            md5hex =str.getBytes();
            return new BigInteger(bytesToHex(md5hex), 16);
        }
    }

    public static Long stringToLongOfMd5(String str) {
        byte[] md5hex = messageDigest.digest(str.getBytes());
        return new BigInteger(bytesToHexOf24(md5hex), 20).longValue();
    }

    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }


    public static String bytesToHexOf24(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArrayOf20[v >>> 4];
            hexChars[j * 2 + 1] = hexArrayOf20[v & 0x0F];
        }
        return new String(hexChars);
    }

}
